<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style>
    /*重新设置样式*/
    .form-control {
        width: 196px !important;
        height: 34px !important;
        padding: 10px !important;
    }
</style>
<body class="hold-transition skin-blue sidebar-mini" onload="checkBrowser()">
<section class="content-header">
    <h1>入库单</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="inBillForm" class="form-horizontal" role="form"
                          enctype="multipart/form-data">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group"
                                     style="line-height: 40px;margin: 0px;padding: 0px;display: none">
                                    <label>制单人</label>
                                    <input type="text" id="odrMakePerson" name="odrMakePerson"
                                           th:value="${odrMakePerson}" class="form-control" readonly="readonly"/>
                                </div>
                                <div class="form-group" style="line-height: 40px;margin: 0px 0px 0px 10px;padding: 0px">
                                    <label>摘要</label>
                                    <input type="text" id="odrRemark" name="odrRemark" class="form-control"
                                           placeholder="请输入摘要"/>
                                </div>
                                <div class="form-group" style="line-height: 40px;margin: 0px 0px 0px 10px;padding: 0px">
                                    <label>单据类型</label>
                                    <select id="odrOutType" name="odrOutType" class="form-control select"
                                            style="padding: 0px!important;text-align: center!important;text-align-last: center!important;">
                                        <option value="">请选择单据类型</option>
                                        <option value="1">采购入库</option>
                                        <option value="2">车间返料</option>
                                        <option value="3">串料剩余</option>
                                        <option value="4">项目完结剩余</option>
                                    </select>
                                </div>
                                <div class="form-group" style="line-height: 40px;margin: 0px 0px 0px 10px;padding: 0px">
                                    <label>往来部门</label>
                                    <select id="odrDept" name="odrDept" class="form-control select2"
                                            style="padding: 0px!important;text-align: center!important;text-align-last: center!important;">
                                    </select>
                                </div>
                                <div shiro:hasAnyPermissions="purwar:purinwar:edit,mecwar:inwar:edit,ykwar:inwar:edit" class="form-group pull-right"
                                     style="line-height: 40px;text-align:center">
                                    <label>模板下载</label>
                                    <label><a style="text-decoration:underline"
                                              th:href="@{/file/外购件入库单据导入模板.xlsx}">点击下载</a></label>
                                </div>
                                <!--<div style="display: none" class="form-group">-->
                                    <!--<input type="hidden" id="serialNumber" th:value="${serialNumber}"/>-->
                                <!--</div>-->
                            </div>
                        </div>
                        <!--<div class="row" style="margin-top: 5px">-->
                        <!--<div class="form-inline">-->
                        <!---->
                        <!--&lt;!&ndash;<div class="form-group col-sm-9">&ndash;&gt;-->
                        <!--&lt;!&ndash;<label>上传入库单：</label>&ndash;&gt;-->
                        <!--&lt;!&ndash;<input id="uploadExcelFile" type="file" class="form-control" style="border: 0"&ndash;&gt;-->
                        <!--&lt;!&ndash;name="uploadExcelFile">&ndash;&gt;-->
                        <!--&lt;!&ndash;<button shiro:hasPermission="purchase:purWarInManage" id="btn-upload" type="button"&ndash;&gt;-->
                        <!--&lt;!&ndash;class="btn btn-success">&ndash;&gt;-->
                        <!--&lt;!&ndash;&lt;!&ndash;<i class="fa fa-upload"></i>&ndash;&gt;&ndash;&gt;-->
                        <!--&lt;!&ndash;<span class="Bold">&nbsp;上传</span>&ndash;&gt;-->
                        <!--&lt;!&ndash;</button>&ndash;&gt;-->
                        <!--&lt;!&ndash;</div>&ndash;&gt;-->

                        <!--</div>-->
                        <!--</div>-->
                    </form>
                </div>
                <div class="box-body">
                    <div id="hot"/>
                </div>
                <div class="box-footer">
                    <div class="row">
                        <div class="col-md-2">
                            <label>制单人：</label><label th:text="${odrMakePerson}"></label>
                        </div>
                        <div class="col-md-6">
                            <label></label>
                        </div>
                        <div class="col-md-4">
                            <div class="col-md-2 col-md-offset-7">
                                <!--<button type="button" class="btn btn-info file-contract btn-file">打印</button>-->
                            </div>
                            <div class="col-md-2">
                                <button type="button" id="btn-save" style="width: 82px;height: 34px"
                                        shiro:hasAnyPermissions="purwar:purinwar:edit,mecwar:inwar:edit,ykwar:inwar:edit"
                                        class="btn btn-success file-contract btn-file">保存
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <!--<div style="text-align: right;margin-top: 10px">-->
                <!--<button type="button" id="btn-save" style="width: 82px;height: 34px"-->
                <!--shiro:hasPermission="purwar:purinwar:edit"-->
                <!--class="btn btn-success file-contract btn-file">保存-->
                <!--</button>-->
                <!--</div>-->
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    /**
     * 检测浏览器
     * */
    function checkBrowser() {
        if (!checkBrowserFun()) {
            alert("尊敬的客户，您使用浏览器不是谷歌浏览器，请使用谷歌浏览器访问本系统！");
            location.href = ctx + "overview/index";
        }
    }
    function checkBrowserFun() {
        let is360 = false;
        let isIE = false;
        let isFirefox = false;
        let isChrome = false;
        let isEdge = false;
        let broName = 'Runing';
        let str = '';
        let strStart = 0;
        let strStop = 0;
        let arr = new Array();
        let temp = '';
        let userAgent = window.navigator.userAgent; //包含以下属性中所有或一部分的字符串：appCodeName,appName,appVersion,language,platform
        /*alert(userAgent);*/
        //FireFox
        if (userAgent.indexOf('Firefox') != -1) {
            isFireFox = true;
            /*broName = 'FireFox浏览器';*/
            strStart = userAgent.indexOf('Firefox');
            temp = userAgent.substring(strStart);
            broName = temp.replace('/', '版本号')

        }
        //Edge
        if (userAgent.indexOf('Edge') != -1) {
            isEdge = true;
            /*broName = 'Edge浏览器';*/
            strStart = userAgent.indexOf('Edge');
            temp = userAgent.substring(strStart);
            broName = temp.replace('/', '版本号');
        }
        //IE浏览器
        if (userAgent.indexOf('NET') != -1 && userAgent.indexOf("rv") != -1) {
            isIE = true;
            /*broName = 'IE浏览器'; */
            strStart = userAgent.indexOf('rv');
            strStop = userAgent.indexOf(')');
            temp = userAgent.substring(strStart, strStop);
            broName = temp.replace('rv', 'IE').replace(':', '版本号');
        }
        //360极速模式可以区分360安全浏览器和360极速浏览器
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") < 0 && userAgent.indexOf("Firefox") < 0) {
            if (navigator.javaEnabled()) {
                is360 = true;
                broName = '360安全浏览器-极速模式';
            } else {
                is360 = true;
                broName = '360极速浏览器-极速模式';
            }
        }
        //360兼容
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") != -1 && userAgent.indexOf("MSIE") != -1 && userAgent.indexOf("rv") < 0) {
            is360 = true;
            broName = '360兼容模式';
        }
        //Chrome浏览器
        if (userAgent.indexOf('WOW') < 0 && userAgent.indexOf('Chrome') > 0 && userAgent.indexOf("Edg") < 0) {
            isChrome = true;
            /*broName = 'Chrome浏览器';*/
            strStart = userAgent.indexOf('Chrome');
            strStop = userAgent.indexOf(' Safari');
            temp = userAgent.substring(strStart, strStop);
            broName = temp.replace('/', '版本号');
        }
        return isChrome;
    }
    /**
     * 验证日期格式
     * @param date
     * @return {boolean}
     */
    function checkDate(date) {
        var res = false;
        if (date != undefined && date != null && date != '') {
            var result = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
            if (result == null)
                return false;
            var d = new Date(result[1], result[3] - 1, result[4]);
            res = (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4]);
        } else {
            res = true;
        }
        if (res == false) {
            js.modal.warning("红色背景色的记账日期格式错误，请修改。正式的格式是：“年\\月\\日”。例如：“2020\\06\\01”。还有请按照正常日历的年份、月份和日期填写。");
        }
        return res;
    }

    $(function () {
        $("title").html($("title").html()+" —— 外购件采购入库");
        //仓库数组
        var warehouseInfoArray;
        //入库类型
        var oadItyStockTypeArray = ["库存在库", "项目在库"];

        /**
         *@FunctionName:  js.ajax
         *@Description: 加载仓库下拉框内容
         *@DesignIdea: 通过JavaScript异步加载技术获取仓库下拉框所需的数据
         *@Author: 徐一贺
         *@CreateDate: 2020/3/18 14:10
         *@Phone: 18241927380
         *@Param: [ajax所需基本参数]
         *@Version: v 1.0.0
         */
        js.ajax({
            url: ctx + 'common/warehouseInfo/list',
            type: 'post',
            traditional: true,            // 阻止深度序列化， 使参数可以使用数组
            dataType: 'json',
            contentType: "application/x-www-form-urlencoded",
            data: {},
            async: false,                // 异步执行
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    warehouseInfoArray = new Array();

                    var mode = [[${mode}]];
                    for (var i = 0; i < result.data.rows.length; i++) {
                        if(mode == "pur" && (result.data.rows[i].whiTypePid == 1)){
                            warehouseInfoArray.push(result.data.rows[i].whiName);
                        }else if (mode == "mec" && (result.data.rows[i].whiTypePid == 3)) {
                            warehouseInfoArray.push(result.data.rows[i].whiName);
                        }else if(mode == "yk" && (result.data.rows[i].whiTypePid == 2)){
                            warehouseInfoArray.push(result.data.rows[i].whiName);
                        }
                        // if (mode == "pur" && result.data.rows[i].whiCode != "机械备件库" && result.data.rows[i].whiCode != "机械备件库-营口" &&
                        //     result.data.rows[i].whiCode != "机械备件库消耗品库" && result.data.rows[i].whiCode !="机械油漆库" &&
                        //     result.data.rows[i].whiCode != "机械油漆库-营口" && result.data.rows[i].whiCode != "机械备件库消耗品库-营口" &&
                        //     result.data.rows[i].whiCode !="机械备件库液压库" && result.data.rows[i].whiCode !="机械备件库结晶器库") {
                        //     warehouseInfoArray.push(result.data.rows[i].whiName);
                        // } else if (mode == "mec" && (result.data.rows[i].whiCode == "机械备件库" || result.data.rows[i].whiCode == "机械备件库消耗品库" ||
                        //     result.data.rows[i].whiCode == "机械油漆库" || result.data.rows[i].whiCode == "机械备件库液压库" || result.data.rows[i].whiCode == "机械备件库结晶器库")) {
                        //     warehouseInfoArray.push(result.data.rows[i].whiName);
                        // } else if (mode == "yk" && (result.data.rows[i].whiCode == "机械备件库-营口" || result.data.rows[i].whiCode == "机械油漆库-营口" ||
                        //     result.data.rows[i].whiCode == "机械备件库消耗品库-营口")) {
                        //     warehouseInfoArray.push(result.data.rows[i].whiName);
                        // }
                    }
                } else {
                    js.modal.warning(result.msg);
                }
            },
            error: function () {
                js.modal.fail();
            }
        });
        // 加载往来部门列表
        js.post({
            url: ctx + "purWarOutManage/findDeptList",
            data: {},
            contentType: "application/json",
            success: function (result) {
                if (result.type == 'SUCCESS') {
                    var list = result.data;
                    $('#odrDept').empty();
                    $('#odrDept').append('<option style="vertical-align: middle;text-align: center" value="">请选择</option>');
                    for (var i = 0; i < list.length; i++) {
                        $('#odrDept').append('<option style="vertical-align: middle;text-align: center" value="' + list[i].depId + '">' + list[i].depName + '</option>');
                    }
                }
            }
        });
        var process = function (data) {
            $("#serialNumber").hide();
            if ($("#queryForm [name='odrRemark']").val() == undefined) {
                $("#queryForm [name='odrRemark']").val("");
            }

            if ($.isEmptyObject(data.insertData)) {
                js.modal.warning("入库明细不允许为空！");
                return;
            }
            var checkData = data.insertData;
            var errorInfoArray = new Array();
            var errorCount = 0;
            if (data.formData.odrOutType == undefined) {
                js.modal.warning("请选择入库类型！");
                return;
            }
            // if(data.formData.odrDept==undefined){
            //     js.modal.warning("请选择往来部门！");
            //     return;
            // }
            for (var i = 0; i < checkData.length; i++) {
                // if (checkData[i].odaSapCode == undefined || checkData[i].odaSapCode == "") {
                //     errorInfoArray.push("第" + (i + 1) + "行 SAP物料编码为空 为空。<br>");
                //     errorCount++;
                // }
                if (checkData[i].odaMatDesc == undefined || checkData[i].odaMatDesc == "") {
                    errorInfoArray.push("第" + (i + 1) + "行 物料描述为空 为空。<br>");
                    errorCount++;
                }
                if (checkData[i].odaNum == undefined || checkData[i].odaNum == "" || checkData[i].odaNum == 0) {
                    errorInfoArray.push("第" + (i + 1) + "行 数量为空 为空。<br>");
                    errorCount++;
                }
                if (checkData[i].odaNumUnit == undefined || checkData[i].odaNumUnit == "") {
                    errorInfoArray.push("第" + (i + 1) + "行 单位为空 为空。<br>");
                    errorCount++;
                }
                if (checkData[i].odaTotalPrice == undefined || checkData[i].odaTotalPrice == null) {
                    errorInfoArray.push("第" + (i + 1) + "行 金额为空 为空。<br>");
                    errorCount++;
                }
                if (checkData[i].odaNumPrice == undefined || checkData[i].odaNumPrice == null) {
                    errorInfoArray.push("第" + (i + 1) + "行 单价为空 为空。<br>");
                    errorCount++;
                }
                // if (checkData[i].odaTrackCode == undefined || checkData[i].odaTrackCode == "") {
                //     errorInfoArray.push("第" + (i + 1) + "行 跟踪号为空 为空。<br>");
                //     errorCount++;
                // }

                if (checkData[i].warehouseLocation == undefined || checkData[i].warehouseLocation == "") {
                    errorInfoArray.push("第" + (i + 1) + "行 仓库为空 为空。<br>");
                    errorCount++;
                }
                // if (checkData[i].odaPosition == "") {
                //     errorInfoArray.push("第" + (i + 1) + "行 区位为空 为空。<br>");
                //     errorCount++;
                // }
                if (checkData[i].odaItyStockType == undefined || checkData[i].odaItyStockType == "") {
                    errorInfoArray.push("第" + (i + 1) + "行 在库类型为空 为空。<br>");
                    errorCount++;
                } else if (checkData[i].odaItyStockType == "项目在库") {
                    if (checkData[i].odaProName == "") {
                        errorInfoArray.push("第" + (i + 1) + "行 项目名称为空 为空。<br>");
                        errorCount++;
                    }
                    if (checkData[i].odaProCode == "") {
                        errorInfoArray.push("第" + (i + 1) + "行 项目编号为空 为空。<br>");
                        errorCount++;
                    }
                } else if (checkData[i].odaItyStockType != "库存在库") {
                    if (checkData[i].odaProCode == "") {
                        errorInfoArray.push("第" + (i + 1) + "行 在库类型错误，应为“项目在库”或“库存在库” 错误。<br>");
                        errorCount++;
                    }
                }
            }
            if (errorCount > 0) {
                errorInfoArray.push("单据保存失败，请先维护以上外购件信息！");
                js.modal.warning(errorInfoArray.join(''));
                $("#serialNumber").show();
                return;
            }
            js.post({
                url: ctx + "purWarInManage/create",
                data: JSON.stringify(data),
                contentType: "application/json",
                success: function (result) {
                    if (result.type == "SUCCESS") {
                        location.reload();
                        $("#serialNumber").show();
                        js.modal.success(result.data);
                    } else {
                        $("#serialNumber").show();
                        js.modal.warning(result.data);
                    }
                }
            });
        }
        js.hot.init({
            element: document.getElementById('hot'),
            data: {},
            formData: ["odrCode", "odrRemark", "odrMakePerson", "odrOutType", "odrDept", "serialNumber"],
            colHeaders: ['ID', 'SAP物料编码', '物料描述', '数量', '单位', '金额', '单价', '供应商','跟踪号', '项目名称', '项目编号', '仓库', '区位','箱号', '在库类型', '记账日期', '特殊说明', '机械备件库备注类型','合同编号'],
            fieldName: ['odaId', 'odaSapCode', 'odaMatDesc', 'odaNum', 'odaNumUnit', 'odaTotalPrice', 'odaNumPrice', 'odaBrand','odaTrackCode', 'odaProName', 'odaProCode', 'warehouseLocation', 'odaPosition','odaCaseNo', 'odaItyStockType', 'odaBookkeepingDate', 'odaSpec','odaRemarkType','contractNo'],
            height: 640,
            minSpareRows: 2000,
            hiddenColumns: {
                columns: [0],
                indicators: true
            },
            callBack: process,
            columns: [
                {
                    data: 'odaId',
                    type: 'numeric',
                    width: 40
                },
                {
                    data: 'odaSapCode',//SAP物料编码
                    type: 'text'
                },
                {
                    data: 'odaMatDesc',//物料描述
                    type: 'text'
                },
                {
                    data: 'odaNum',//数量
                    type: 'numeric'
                },
                {
                    data: 'odaNumUnit',//单位
                    type: 'text'
                },
                {
                    data: 'odaTotalPrice',//金额
                    type: 'numeric'
                },
                {
                    data: 'odaNumPrice',//单价
                    type: 'numeric'
                },
                {
                    data: 'odaBrand',//供应商
                    type: 'text'
                },
                {
                    data: 'odaTrackCode',//跟踪号
                    type: 'text'
                },
                {
                    data: 'odaProName',//项目名称
                    type: 'text'
                },
                {
                    data: 'odaProCode',//项目编号
                    type: 'text'
                },
                {
                    data: 'warehouseLocation',//仓库
                    type: 'dropdown',
                    source: warehouseInfoArray
                },
                {
                    data: 'odaPosition',//区位
                    type: 'text'
                },
                {
                    data: 'odaCaseNo',//箱号
                    type: 'text'
                },
                {
                    data: 'odaItyStockType',//入库类型
                    type: 'dropdown',
                    source: oadItyStockTypeArray
                },
                {
                    data: 'odaBookkeepingDate',//记账日期
                    type: 'date',
                    dateFormat: 'YYYY/MM/DD',
                    correctFormat: true,
                    validator(val, callback) {
                        let pattern = /^.{0,20}$/
                        if (checkDate(val)) return callback(true)
                        return callback(false)
                    }
                },
                {
                    data: 'odaSpec',//特殊说明
                    type: 'text'
                },
                {
                    data: 'odaRemarkType',//机械备件库备注类型
                    type: 'text'
                },
                {
                    data: 'contractNo',//合同编号
                    type: 'text'
                }
            ]
        });


    });
</script>
</body>
</html>
